package com.anxin.act.task.vo;

import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.StringUtils;
import org.activiti.engine.form.FormProperty;

import java.util.*;

/**
 * 待办表单配置
 *
 * @author: liuxiangyu
 * @date: 2020/4/3 10:26
 */
public class FormPropertyVo {

    // 字段标识
    private String id;
    // 字段名称
    private String name;
    // 字段类型
    private String type;
    //字段值
    private String value;
    // 必填
    private boolean isRequire;
    // 是否展示快捷意见
    private String quickAdvice;


    //下拉框的字典
    private List<Map<String,String>> option = new ArrayList<>();

    public FormPropertyVo(){

    }

    public FormPropertyVo(Map<String, Object> map){
        this.id = map.get("key").toString();
        this.name = map.get("value").toString();
        this.isRequire = true;
        this.type = "string";
        this.quickAdvice = "N";


    }


    /**
     * 构造方法
     * @param formProperty formProperty
     * @param taskId 任务ID
     */
    public FormPropertyVo(FormProperty formProperty,String taskId){
        this.id = formProperty.getId();
        this.name = formProperty.getName();
        this.type = formProperty.getType().getName();
        //this.value = formProperty.getValue();
        this.isRequire = formProperty.isRequired();

        if ("enum".equals(this.type)){

            Map values = (Map) formProperty.getType().getInformation("values");
            Map formPropertyMap = new LinkedHashMap();
            values.forEach(formPropertyMap::put);


            Set keySet = formPropertyMap.keySet();

            // 不可以驳回的节点，将 驳回 改为 不同意
            if (formPropertyMap.get("驳回") != null){

                boolean rejected = ActUtils.rejectedCheck(taskId);

                if (!rejected){
                    formPropertyMap.remove("驳回");
                    formPropertyMap.putIfAbsent("不同意", "不同意");
                }

            }

            for (Object key : keySet) {
                if (StringUtils.isBlank(this.value) && formProperty.isWritable()){
                    this.value = key.toString();
                }
                HashMap<String, String> keyMap = new HashMap<>();
                keyMap.put("key",key.toString());
                keyMap.put("value",formPropertyMap.get(key).toString());
                option.add(keyMap);

            }


        }
    }

    /**
     * 获取表单属性展示值
     * @return 表单属性展示值
     */
    public String getValueText(){
        if ("enum".equals(this.type)){
            for (Map<String, String> optionMap : this.option) {
                if (optionMap.get("key").equals(this.value)){
                    return optionMap.get("value");
                }
            }
        }

        return this.value;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public boolean isRequire() {
        return isRequire;
    }

    public void setRequire(boolean require) {
        isRequire = require;
    }

    public List<Map<String, String>> getOption() {
        return option;
    }

    public void setOption(List<Map<String, String>> option) {
        this.option = option;
    }

    public String getQuickAdvice() {
        return quickAdvice;
    }

    public void setQuickAdvice(String quickAdvice) {
        this.quickAdvice = quickAdvice;
    }
}
